require([], function() {
	var content = $("._content");
	var items = $(".weui-tabbar > a");

	items.on("click", function() {
		if(!$(this).is(".weui-bar__item_on")) {
			location.hash = "#" + $(this).data("do");
		}

	});

	$(window).on("hashchange", function() {
		if(location.hash == "") {
			items.eq(0).trigger("click");
			return ;
		}

		var _do = location.hash.substr(1);
		
		var loading = weui.loading("页面加载中");

		var $this = items.filter("[data-do='" + _do + "']");
		if($this.length > 0) {
			var other = items.filter(".weui-bar__item_on");
			other.removeClass("weui-bar__item_on");
			var span = other.children("span");
			span.removeClass(span.data("on")).addClass(span.data("off"));
			$this.addClass("weui-bar__item_on");
			span = $this.children("span");
			span.removeClass(span.data("off")).addClass(span.data("on"));
		}
		
		$.get(
			_do+".html?t="+new Date().getTime(),
			function(data){
				content.html(data);
				$(window).scrollTop(0);
				require([_do],function(fn){
					fn(content,loading);
				})
			}
		);
	});

	
	if(window.location.hash == "") {
		items.eq(0).trigger("click");
	} else {
		$(window).trigger("hashchange");
	}
});


/**
 * 将日期对象格式化成字符串
 * @param {String} format 格式支持：yyyy-MM-dd HH:mm:ss.S 默认：yyyy-MM-dd
 */
Date.prototype.format = function(format)
{
		format = format==null?"yyyy-MM-dd":format;
		var o = 
		{
			"M+" : this.getMonth()+1, //month
			"d+" : this.getDate(),    //day
			"H+" : this.getHours(),   //hour
			"m+" : this.getMinutes(), //minute
			"s+" : this.getSeconds(), //second
			"q+" : Math.floor((this.getMonth()+3)/3), //quarter
			"S" : this.getMilliseconds() //millisecond
		}
		
		if(/(y+)/.test(format)) 
		format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
		for(var k in o)
		if(new RegExp("("+ k +")").test(format))
		format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
		return format;
}

/**
 * 将字符串格式化成日期对象
 * @param {String} format 格式支持：yyyy-MM-dd HH:mm:ss.S 默认：yyyy-MM-dd
 */
String.prototype.toDate = function(format){
	format = format==null?"yyyy-MM-dd":format;
	var d = new Date();
		var o = 
		{
			"y+" : "d.setYear(val)",	//year
			"M+" : "d.setMonth(val-1)", //month
			"d+" : "d.setDate(val)",    //day
			"H+" : "d.setHours(val)",   //hour
			"m+" : "d.setMinutes(val)", //minute
			"s+" : "d.setSeconds(val)", //second
			"S+" : "d.setMilliSeconds(val)" //millisecond
		}
	var regx = format
	for(var k in o){
		if(new RegExp("("+ k +")").test(format))
			regx = regx.replace(new RegExp(k),"[0-9]+");
	}
	
	if(!new RegExp("^"+regx+"$").test(this)){
		throw "日期格式不正确:"+format;
	}
	
	for(var k in o){
		if(new RegExp("("+ k +")").test(format)){
			var regx = format;
			for(var temp in o){
				if(temp == k){
					regx = regx.replace(new RegExp(temp),"([0-9]+)");
				}else{
					regx = regx.replace(new RegExp(temp),"[0-9]+");
				}
				
			}
			var val = Number(this.replace(new RegExp("^"+regx+"$"),"$1"));
			try{
				
				if(k=="S+"){
					d.setMilliseconds(val);
				}else{
					eval(o[k].replace("val",val));
				}
			}catch(e){alert(e);}
		}else{
			try{
				var val = k=="M+"?1:0;
				if(k=="S+"){
					d.setMilliseconds(val);
				}else{
					eval(o[k].replace("val",val));
				}
			}catch(e){alert(e);}
		}
	}
	return d;
}